# 文件名: cickhouse
# 当前用户: shiping.zheng
# 当前日期: 2024/9/13
# 当前时间: 17:43
# 项目名称: Flag
import clickhouse_connect
"""
模块描述:
    
"""
class ClickHouseConnector:
    def __init__(self, host='10.51.2.30', port=8123, username=None, password=None, database=None):
        self.host = host
        self.port = port
        self.username = username
        self.password = password
        self.database = database
        self.client = self.connect()

    def connect(self):
        return clickhouse_connect.get_client(
            host=self.host,
            port=self.port,
            username=self.username,
            password=self.password,
            database=self.database
        )

    def execute_query(self, sql):
        try:
            result = self.client.query(sql)
            if result.result_rows:
                return result.result_rows  # 返回查询结果
            else:
                return None  # 增删改操作成功
        except Exception as e:
            return f"Operation failed: {str(e)}"  # 返回错误信息

    def execute_queries(self, sql_list):
        """
        执行多条 SQL 语句
        :param sql_list: 包含 SQL 语句的列表
        """
        results = []
        for sql in sql_list:
            try:
                result = self.client.query(sql)
                if result.result_rows:
                    results.append(result.result_rows)
                else:
                    results.append("Operation successful")
            except Exception as e:
                results.append(f"Operation failed: {str(e)}")
        return results

